package com.atguigu.gulimall.order.web;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
public class HelloController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/testSubmit")
    public String testSubmitOrder() {
        //订单下单成功
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(UUID.randomUUID().toString());
        orderEntity.setModifyTime(new Date());

        rabbitTemplate.convertAndSend("order-event-exchange", "order.create.order", orderEntity);
        return "ok";
    }

    @RequestMapping("aa")
    public void sendMessage() {
        //发送消息
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setNote("这是一首简单的小情歌");
        rabbitTemplate.convertAndSend("order-event-exchange", "order.create.order", orderEntity);
        log.info("消息发送完成--", orderEntity);
    }
//    @RabbitListener(queues = {"order.release.order.queue"})
    public void receiveMessage(OrderEntity orderEntity, Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
        System.out.println(orderEntity);
        System.out.println("接收到消息--getBody：" + message.getBody());


    }
}
